<!DOCTYPE html>
<html>
	<head>
	<title>宣传培训</title>
		<meta http-equiv="Pragma" CONTENT="no-cache">
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<meta name="renderer" content="webkit">

		<%include("/common/include.html"){}%>
		<style type="text/css">
			.contentx ul li{
				display:inline-block;
				margin-top:20px;
				width: 500px;
			}
		</style>
	</head>
	<body>
		<form id="propaganda"   method="post"  enctype="multipart/form-data">
			<div class="easyui-layout">
				<div id="contentx" style="width: 500px;height: auto;margin:auto;">
					<ul>
						<li style="margin-left: 0px;">
							<span style="font-weight: bold;">活动时间</span>
							<input name="bpropaganda.time" value="date()" class="easyui-datebox theme-textbox-radius" data-options="sharedCalendar:'#cc'" style="width: 80%;height: 40px;">
						</li><br>
						<div id="cc" class="easyui-calendar"></div>
						<li>
							<span style="font-weight: bold;">活动地点</span>
							<input name="bpropaganda.place"  class="easyui-textbox theme-textbox-radius" style="width: 80%;height:40px">
						</li><br>
						<li>
							<span style="font-weight: bold;">活动方式</span>
							<#code type="select" id="activityWay_select" name="bpropaganda.way" code="code_activityWay"  class="easyui-combobox inputBox theme-textbox-radius" style="width: 80%;"/>								
						</li><br>
						<li>
							<span style="font-weight: bold;">参加人员</span>
							<a href="javascript:void(0);" class="easyui-linkbutton" style="height: 32px;width:100px;background: #00A65A;color: white;" onclick="$('#w').window('open')">请&nbsp;&nbsp;选&nbsp;&nbsp;择</a>								
						</li><br>					
						<li>
							<span style="font-weight: bold;">所用物品</span>
							<a href="javascript:void(0);" class="easyui-linkbutton" style="height: 32px;width:100px;background: #00A65A;color: white;" onclick="$('#w2').window('open')">请&nbsp;&nbsp;选&nbsp;&nbsp;择</a>
						</li><br>
						<li>
							<span style="font-weight: bold;">宣传内容</span>
							<input id = "test" name="bpropaganda.propagandas" class="easyui-textbox theme-textbox-radius" data-options="multiline:true"  style="width:80%;height:100px">
						</li>
						<li style="text-align:center;margin-top:30px;">
							<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="save()"style="height:32px;">保存</a>
						</li>
					</ul>
				</div>
			</div>
		</form>
		
	  <!--参加人员开头 -->	   
		<div id="w" class="easyui-window" title="请选择" data-options="collapsible:false,minimizable:false,maximizable:false,closable:true,resizable:true" style="width:750px;height:500px;padding:5px;" data-options="cls:'theme-panel-orange'" closed="true">
			<div class="easyui-layout" data-options="fit:true">
				<form id="allData_form">
					<div data-options="region:'west'" style="width:20%">
						<%include("/bpropagandaTraining/propTree.html", {'gridId':'deptTable'}){}%>
					</div>
					<div data-options="region:'center'">
						<table id="deptTable" style="height:100%"></table>
					</div>
					<div data-options="region:'south'" style="height:50px;position:relative;">
						<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="confirm()" style="position:absolute;right:10px;top:8px;">确定</a>
					</div>
				</form>
			</div>
		</div>	
		<!-- 参加人员结尾-->
		<!-- 所用物品开头 --> 	   
		<div id="w2" class="easyui-window" title="请选择" data-options="collapsible:false,minimizable:false,maximizable:false,closable:true,resizable:false" style="width:780px;height:500px;padding:5px;" data-options="cls:'theme-panel-orange'" closed="true">
			<div class="easyui-layout" data-options="fit:true">
				<form id="allData_formx">
					<div data-options="region:'center'">						
						<table id="goodTable" style="height:100%"></table>
						<div id="allDatax" style="display:none;"></div>
					</div>
					<div data-options="region:'south',border:true" style="height:50px;position:relative;">
						<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" style="position:absolute;right:10px;top:8px;" onclick="confirmGood()">确定</a>
					</div>
				</form> 
			</div>
		</div>	
		<!-- 所用物品结尾 -->		
		<script src="${cxt !}/sb/js/jquery.form.js" type="text/javascript" charset="utf-8"></script>
	<script>
	//页面加载时
	window.onload = function(){
		var winH = $(window).height();
		var contentx = document.getElementById("contentx");
		var contentxH = $("#contentx").height();
		var padding=(winH-contentxH)/2;
		contentx.style.marginTop=padding+"px"; 
	}
	//窗口大小改变时
	window.onresize = function(){
		var winH = $(window).height();
		var contentx = document.getElementById("contentx");
		var contentxH = $("#contentx").height();
		var padding=(winH-contentxH)/2;
		contentx.style.marginTop=padding+"px"; 
	}	
	//数据表格复选框全选
	function selectAllBox(){  
		  $('#deptTable').datagrid('selectAll');    
	}  
	$(document).ready(function () {
		initGoodTable();
	})
	function initDeptTable(t){

		$('#deptTable').datagrid({
			cls: "theme-datagrid",
			title: "人员列表",
			rownumbers: true, //显示序号
			pagination: false,
			fitColumns:true,
			//pageSize: 10,
			url: "departmentUser/personnelList",
			queryParams: {
				'_query.bh': t 
			},
		    method: 'post',
			collapsible: true,
			columns:[[
						{field:'check',checkbox:true},
				  		{field:'ids',title:'ids',width:90,align:'center', hidden:true},
				  		{field:'username',title:'用户名',width:90,align:'center', hidden:true},
				  		{field:'name',title:'姓名',width:90, align:'center'
				  			/*,
				  			formatter: function(value,row,index){
				  				var ids = row.ids;
								return '<a href="#" style="color: blue;" class="button-edit" onclick="lookInfo(\'' + ids + '\')">' + value + '</a>';
							}*/
				  		},
				  		{field:'dname',title:'部门',width:90, align:'center'},
				  		{field:'stationname',title:'岗位',width:90, align:'center'},									  		
				  		{field:'mobile',title:'联系方式',width:90, align:'center'},
				  		{field:'status',title:'状态',width:90, align:'center', hidden:true
				  			/*formatter: function(value,row,index){
				  				var status = row.status;
				  				if(status == 1){
				  					return '正常';
				  				}else{
				  					return '禁用';
				  				}
				  			}*/
				  		}
				      ]],
			onLoadSuccess:selectAllBox
		});
	}
	function initGoodTable(){	
		 $("#goodTable").datagrid({
			cls: "theme-datagrid",
			singleSelect: true,
			selectOnCheck:$(this).is(':checked'),
			checkOnSelect:$(this).is(':checked'),
			rownumbers: true, //显示序号
			pagination: false,
			fitColumns:true,
			//pageSize: 10,
			url:"propgandaThings/productList",
			onClickRow: onClickRow,
		    method: 'post',
			collapsible: true,
			columns:[[
					  
			         // {field:'date',title:'日期',width:160,align:'center',editor:'datebox'},
				  	  {field:'ids',title:'ids',width:90,align:'center', hidden:true},
				  	  {field:'orgids',title:'orgids',width:90,align:'center', hidden:true},
				  	  {field:'probh',title:'编号',width:90,align:'center'},
				  	  {field:'name',title:'名称',width:90, align:'center'},
				  	  {field:'price',title:'单价',width:90, align:'center'},
				  	  {field:'num',title:'库存量',width:90, align:'center'},
				  	  {field:'pricesum',title:'总额',width:90, align:'center', hidden:true},
				  	  {field:'outStorage',title:'出库数量',width:90, align:'center',
				  		editor:{type:'numberspinner',
							min:0,
							options:{
							 onChange : function(n){
								 rest(n);
							 }
						  }					
					    }
				  	  },
				  	  {field:'rest',title:'剩余库存',width:100, align:'center'}
				      ]],
			onLoadSuccess:function(){
				
			}
			
		}); 
	}
	//确认所需物品
	function confirmGood(){
		/*把所有数据都拼接到form表单里*/
			//将出库记录保存起来
		$("#allDatax").html("");
		var rows = $('#goodTable').datagrid('getChanges');
		
		var name1 = "";
		for(var i=0;i<rows.length;i++)
		{
		//	var date = rows[i].date;
			var thingids = rows[i].ids;
			var orgids = rows[i].orgids;
			var probh = rows[i].probh;
			var name = rows[i].name;	    
			var price = rows[i].price;
			var num = rows[i].num;
			var outStorage = rows[i].outStorage;
			var rest = rows[i].rest;
			if((rest=="" || rest==null) && rest<0){
				$.alert(parent.$, name+"库存不足");
			}
			 if(outStorage!=null&&outStorage!=0&&rest!=null&&rest!=""){
				name1 = name1+rows[i].name+",";
			//	$("#allDatax").append("<input type='hidden' name='propagandaThingsOutList["+i+"].date' value='"+date+"'/>");
				$("#allDatax").append("<input type='hidden' name='propagandaThingsOutList["+i+"].thingids' value='"+thingids+"'/>");
				$("#allDatax").append("<input type='hidden' name='propagandaThingsOutList["+i+"].orgids' value='"+orgids+"'/>");
				$("#allDatax").append("<input type='hidden' name='propagandaThingsOutList["+i+"].probh' value='"+probh+"'/>");
				$("#allDatax").append("<input type='hidden' name='propagandaThingsOutList["+i+"].name' value='"+name+"'/>");
				$("#allDatax").append("<input type='hidden' name='propagandaThingsOutList["+i+"].price' value='"+price+"'/>");
				$("#allDatax").append("<input type='hidden' name='propagandaThingsOutList["+i+"].num' value='"+num+"'/>");
				$("#allDatax").append("<input type='hidden' name='propagandaThingsOutList["+i+"].outStorage' value='"+outStorage+"'/>");
				//$("#allDatax").append("<input type='hidden' name='propagandaThingsOutList["+i+"].pricesum' value='"+pricesum+"'/>");
				$("#allDatax").append("<input type='hidden' name='propagandaThingsOutList["+i+"].rest' value='"+rest+"'/>");			
			}
		}
		$("#propaganda").append("<input type='hidden' name='bpropaganda.goods' value='"+name1+"'/>");
		$('#w2').window('close');
	}
	//确认参加人员
	 function confirm(){		
		
		$('#w').window('close');
	} 
	</script>					
	<script type="text/javascript">
		function  save()
			{
			//保存参加人员
			var checkRows=$('#deptTable').datagrid('getChecked');
			var name = "";
			for(var i=0;i<checkRows.length;i++)
			{	
				 $("#allData").html("");
				 var rowIndex = $('#deptTable').datagrid('getRowIndex', checkRows[i]);
				 $('#deptTable').datagrid('endEdit', rowIndex);
				//var ids = checkRows[i].ids;			
				name = name+checkRows[i].name+",";		
			}		
			$("#propaganda").append("<input type='hidden' name='bpropaganda.personnel' value='"+name+"'/>");
			 
			//保存除宣传品以外的信息
					$.ajax({
		                cache: true,
		                type: "POST",
		                url:"${cxt !}/bpropagandaTraining/savePro",
		                data:$('#propaganda').serialize(),// 你的formid
		                dataType:'json',
		                async: false,
		                success: function(result) {		                	
		                	if(result.msg == "200")
		                	{	
		                		$.slideMsg("保存成功", parent.$);
		                		//保存宣传品及出库信息
		                		 var url1='/bpropagandaTraining/outGoods';
		         				var data1=$('#allData_formx').serialize();
		         				
		         				var reData1 = common_ajax.ajaxFunc(url1,data1);
		         				if(reData1 == "200")
		         				{
		         					 $.slideMsg("保存成功", parent.$);
		         					 parent.refresh();
		         				}else
		         				{
		         					 
		         					$.alert(parent.$,"本次宣传活动无宣传品出库");
		         					 parent.refresh();
		         				}
		                	}else{		                		
		        					 $.alert(parent.$,result.msg);
		                	}
		                }
		            });
				}
		/*数据表格行编辑*/
		var editIndex1 = undefined;
		function endEditing(){
			if (editIndex1 == undefined){return true}
			if ($('#goodTable').datagrid('validateRow', editIndex1)){
				$('#goodTable').datagrid('endEdit', editIndex1);
					editIndex1 = undefined;
					return true;
			} else {
				return false;
			}
		}
		function onClickRow(index){
			if (editIndex1 != index){
				if (endEditing()){
					$('#goodTable').datagrid('selectRow', index)
						.datagrid('beginEdit', index);
						editIndex1 = index;
				} else {
					$('#goodTable').datagrid('selectRow', editIndex1);
				}
			}else{
				if($('#goodTable').datagrid('endEdit', editIndex1)){
					$('#goodTable').datagrid('selectRow', editIndex1)
					.datagrid('beginEdit', editIndex1);
				}
			}
		}
		//计算剩余库存
		function rest(n)
		{	
			var row = $('#goodTable').datagrid('getSelected');
			var index=$('#goodTable').datagrid('getRowIndex', row);
			var rest=parseInt(row.num)-n;
			if(rest<0){
				$('#goodTable').datagrid('updateRow', {
	                index: index,
	                row: {
	                	outStorage:n,
	                	rest:""
	                }
	            });	
			}else{
				$('#goodTable').datagrid('updateRow', {
	                index: index,
	                row: {
	                	outStorage:n,
	                	rest:rest
	                }
	            });		
			}
		}
		//设置当前日期
		date = function () {
			var date=new Date();
			var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
			var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0"
			+ (date.getMonth() + 1);
			var hor = date.getHours();
			var min = date.getMinutes();
			var sec = date.getSeconds();
			return date.getFullYear() + '-' + month + '-' + day+" "+hor+":"+min+":"+sec;
		};
		</script>
	</body>
</html>

